/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.api.util;

import eu.easyedu.dictionary.api.model.Dictionary;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
import org.openide.util.ContextAwareAction;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;

/**
 *
 * @author hlavki
 */
public abstract class AbstractOpenDictionaryAction extends AbstractAction implements LookupListener, ContextAwareAction {

    private static final long serialVersionUID = 1L;
    protected Lookup context;
    protected Lookup.Result<Dictionary> lookupResult;

    public AbstractOpenDictionaryAction() {
        this(Utilities.actionsGlobalContext());
    }

    public AbstractOpenDictionaryAction(String name) {
        this(Utilities.actionsGlobalContext(), name);
    }

    private AbstractOpenDictionaryAction(Lookup context, String name) {
        super(name);
        this.context = context;
    }

    private AbstractOpenDictionaryAction(Lookup context) {
        super();
        this.context = context;
    }

    protected void init() {
        assert SwingUtilities.isEventDispatchThread() : "this shall be called just from AWT thread";
        if (lookupResult != null) {
            return;
        }
        Lookup.Template<Dictionary> tpl = new Lookup.Template<Dictionary>(Dictionary.class);
        lookupResult = context.lookup(tpl);
        lookupResult.addLookupListener(this);
        resultChanged(null);
    }

    public void resultChanged(LookupEvent ev) {
    }
}
